<template>
  <div>
    <!-- <a-button type="primary" @click="showModal">Open Modal</a-button> -->
    <a-modal 
      v-model:visible="visible" 
      @ok="onSubmit"
      okText="提交"
      cancelText="取消"
      :width="1200"
      title="定额更改-删除"
    >
      <div>
          <a-table
            :row-selection="rowSelection"
            :columns="columns"
            :data-source="data"
          />
      </div>
    </a-modal>
  </div>
</template>
<script setup>
import { computed, unref,ref, onMounted,watchEffect} from 'vue';
import request from '@/utils/request'
const data=ref([])

const visible=ref(false)
const props=defineProps(['quotaReduceList','quotaRecord','count'])
const emit=defineEmits(['handleCount'])

watchEffect(async()=>{
  // console.log(props.count)
  // console.log(props.quotaRecord)
  data.value=props.quotaReduceList

  for(var i=0;i<data.value.length;i++){
    data.value[i].key=i
  }
})
const showModal=()=>{
  visible.value=true
}
defineExpose({
  showModal
})

const selectedRowKeys=ref([])

const rowSelection = ref({
//   checkStrictly: false,
onChange: (selectedRowKeys, selectedRows) => {
// selectedKeys.value=selectedRowKeys
// console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
},
onSelect: (record, selected, selectedRows) => {
console.log(record)
console.log(selected)
selectedRowKeys.value.push(record.id)
// console.log(record);
// console.log( selected);
console.log(selectedRows);
},
onSelectAll: (selected, selectedRows, changeRows) => {
console.log(selected, selectedRows, changeRows);
},
});

  const columns = [{
      title: '车间',
      dataIndex: 'workshopName',
      key: 'workshopName',
      }, {
      title: '班组',
      dataIndex: 'teamName',
      key: 'teamName',
      // width: 80,
      }, {
      title: '编号',
      dataIndex: 'number',
      key: 'number',
      // ellipsis: true,
      }, {
      title: '名称',
      dataIndex: 'toolName',
      key: 'toolName',
      // ellipsis: true,
      }, {
      title: '规格、型号',
      dataIndex: 'modal',
      key: 'modal',
      // ellipsis: true,
      }, {
      title: '试验日期',
      dataIndex: 'testDate',
      key: 'testDate',
      // ellipsis: true,
      }, {
      title: '试验周期',
      dataIndex: 'cycle',
      key: 'cycle',

      // ellipsis: true
      }, {
      title: '试验电压(kv)',
      dataIndex: 'testVoltage',
      key: 'testVoltage',
      // ellipsis: true,
      }, {
      title: '试验人',
      dataIndex: 'tester',
      key: 'tester',
      // ellipsis: true,
      }, {
      title: '审核人',
      dataIndex: 'auditor',
      key: 'auditor',
      // ellipsis: true,
      }, {
      title: '保管人',
      dataIndex: 'keeper',
      key: 'keeper',
      // ellipsis: true,
      }, {
      title: '试验结论',
      dataIndex: 'conclusion',
      key: 'conclusion',
      // ellipsis: true,
      }, {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      // ellipsis: true,
      }, {
      title: '投运日期',
      dataIndex: 'operate_date',
      key: 'operate_date',
      // ellipsis: true,
      }
  ];

  const onSubmit = () => {
      // formState.count=String(formState.count)
      console.log(props.quotaRecord.count)
      console.log(props.count)
      console.log(selectedRowKeys.value)
      console.log(selectedRowKeys.value.length)
      console.log(props.quotaRecord.count - props.count)

      if(data.value.length===0){
        visible.value=false
        const temp=ref(props.quotaRecord.count)
        emit('handleCount',temp.value)
      }
      else if(selectedRowKeys.value.length > (props.quotaRecord.count - props.count)){
        alert("请删除正确的记录条数")
      }
      // console.log(selectedRowKeys.value)
     else{
      console.log(selectedRowKeys.value)
      request({
          url:'/changeQuota/delete',
          method:'post',
          data:{
              id:props.quotaRecord.id,
              toolIdList:selectedRowKeys.value,
              count:props.count
          },
      }).then((res)=>{
          visible.value=false
          selectedRowKeys.value=[]
      }).catch(err => {
      console.log(111111)
      console.log('error', err);
    });
  }
  };
</script>
<style scoped>

 .ant-table-thead > tr > th{
  padding: 0;
}

</style>